<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>模拟 new</title>
</head>
<body>
  <script>
    function customNew(constructor, ...args) {
      // 开辟内存空间
      const obj = {}
      // 将 constructor 的 this 指向 obj
      const result = constructor.call(obj, ...args)
      // 将 obj 的隐式原型指向 constructor 的原型对象
      obj.__proto__ = constructor.prototype
      // 判断构造函数的返回值
      if (typeof result === 'object' && typeof result !== null || typeof result === 'function') {
        return result
      }
      return obj
    }

    function Person(name, age) {
      this.name = name;
      this.age = age;

      return 1
    }
    Person.prototype.do = function () {
      console.log("学习 react")
    }
    const p1 = customNew(Person, "laowang", 18)
    console.log(p1, "p1");

  </script>
</body>
</html>